﻿

function Custom() {
    this.currentMap = null;
    this.mainPath = document.getElementById("mainPath");
    this.currenWave = 0;
    this.resetLevel = function () {
        switch (game.level.currentLevel) {
            case 0:
                break;
            case 1: 
                break;
            case 2:
                break;
        }
    };

    this.initLevel = function () {
        switch (game.level.currentLevel) {
            case 0:
                {
                    //this.mainPath.setAttribute("d", maps[game.level.currentLevel]);
                    this.currentWave = 0;
                    this.currentMap = new Map([0]);
                    // TODO: Make waves a little better
                    this.currentMap.enemyWaves.push(["AAAA", "BB BB", "121212", "123"])
                    this.createPaths();
                    this.setupEnemies();
                    game.content.loadContent(["mainScreen", "map1"], ["thrust"], ["campfire"]);
                    game.board.setBackgroundImage(game.content.imageElement("map1").src);
                    break;
                }
        }
    };
    
    this.endLevel = function () {
        switch (game.level.currentLevel) {
            case 0:
                break;
            case 1: 
                break;
            case 2:
                break;
        }
    };

    this.draw = function () {
        // pre processing
        var currentPath = null;
        var currentPathData = null;
        switch (game.level.currentLevel) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                break;
        }

        game.board.setTime();
        // advance each enemy by evaluating bits on the path
        for (var i = 0; i < this.currentMap.mapPaths.length; i++) {
            currentPath = this.currentMap.mapPaths[i];
            currentPathData = this.currentMap.mapPathTextElement[i].data.toString();
            for (var n = 0; n < currentPathData.length; n++) {
                currentEnemy = parseInt(currentPathData.substr(n, 1));
                // loop through bit wise of each number?
            }
        }

        // post processing
        switch (game.level.currentLevel) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                break;
        }
    };
    this.createPaths = function () {
        var element = null;
        var outerPathGroup = document.getElementById("paths");
        // need to delete first
        //            <g id="paths">
        //                <path id="travelPath0" d="" class="mainPath"/>
        //                <text id="textPath0"> 
        //                    <textPath xlink:href="#travelPath0">...</textPath>
        //                </text>
        //            </g>
        removeChildren(outerPathGroup);
        for (var i = 0; i < this.currentMap.mapPaths.length; i++) {
            element = addElementToDocument("path", outerPathGroup, SVGNS);
            element.setAttribute("id", "travelPath" + i.toString());
            element.setAttribute("class", "mainPath");
            element.setAttribute("d", this.currentMap.mapPaths[i]);
            element = addElementToDocument("text", outerPathGroup, SVGNS);
            element.setAttribute("x", "-100");
            element.setAttribute("class", "textCharacters");
            element.setAttribute("id", "textPath" + i.toString());
            element = addElementToDocument("textPath", element, SVGNS);
            element.setAttributeNS(XLINKNS, "href", "#travelPath" + i.toString());
            this.currentMap.mapPathTextElement[i] = document.createTextNode("");
            element.appendChild(this.currentMap.mapPathTextElement[i]);
        }

    };

    this.setupEnemies = function () {
        for (var i = 0; i < this.currentMap.enemyWaves[this.currentWave].length;i++) {
            // set intial text on path and randomize the map path
            var pathToUse = randomNumber(this.currentMap.mapPaths.length - 1);
            this.currentMap.mapPathTextElement[pathToUse].data = this.currentMap.enemyWaves[i] + this.currentMap.mapPathTextElement[pathToUse].data;

        }
        // pad with spaces

    };

    
    return this;
}